home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / games2 / rotise12.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1992-04-03  |  7KB  |  313 lines

  1. /* misc.c -- Miscellaneous routines
  2.  
  3.     December 1989    Mark E. Mallett
  4.  
  5. This file contains misc routines for the WRAP program.  Some of these
  6. were lifted from FFS.
  7.  
  8. Included are:
  9.  
  10.     asknum        Prompt and ask for a number
  11.     atonum        Convert ASCII to LONG number
  12.     emalloc        malloc() with error message
  13.     erealloc    realloc() with error message.
  14.     error        Handle error message
  15.     getline        Input a line of text from a file.
  16.     gettkline    Input a line of text && tokenize it
  17.     ndtmtotv    Convert date/time digit string to tm struct
  18.     nextonum    Convert ASCII hex to LONG number
  19.     prtdat        Print date
  20.     prtdtm        Print date and time
  21.     prttim        Print time
  22.     prtndat        Print date as a number string
  23.     prtndtm        Print date and time as a number string
  24.     prtntim        Print time as a number string
  25.     vtell        Print according to verbosity level
  26.     warning        Handle warning message.
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #ifdef    __TURBOC__
  32. #include <stdlib.h>
  33. #include <alloc.h>
  34. #else
  35. #include <malloc.h>
  36. #endif
  37. #include <ctype.h>
  38. #include <time.h>
  39.  
  40. #include "rotise.h"
  41.  
  42. #include "bb.h"
  43.  
  44. /* Local Definitions */
  45.  
  46.  
  47. /* External data referenced */
  48.  
  49. extern    int    Vlevel;            /* Current verbosity level */
  50.  
  51. /* External routines used */
  52.  
  53. extern    char    *gettoken();
  54. extern    LONG    os_tmclock();
  55.  
  56. /* Local data publicly available */
  57.  
  58.  
  59. /* Local routines and forward references */
  60. void error PROTO( (int code, char *fmtP, int    a1, int a2, int a3) );
  61.  
  62.  
  63.     long    atonum();
  64.     long    hextonum();
  65.  
  66. /* Private data */
  67.  
  68. static    char    Errbuf[200];        /* Buffer for formatted error text */
  69.  
  70. /*
  71.  
  72. *//* emalloc( facP, nameP, size )
  73.  
  74.     Allocate memory, give error if failure.
  75.  
  76. Accepts :
  77.  
  78.     facP        Name of facility (e.g., subroutine name)
  79.     nameP        Name of item being allocated
  80.     size        Desired size
  81.  
  82. Returns :
  83.  
  84.     < value >    Ptr to thing.
  85.  
  86. Notes :
  87.  
  88.     error return is taken if allocation fails.
  89. */
  90.  
  91. void *
  92. emalloc( facP, nameP, size )
  93.     char        *facP;        /* Ptr to facility name */
  94.     char        *nameP;        /* Ptr to name of thing */
  95.     int        size;        /* Number of bytes */
  96. {
  97.     char        *newP;        /* Ptr to memory */
  98.  
  99.  
  100.     if ( ( newP = malloc( size ) ) == NULL )
  101.     error( EC_MEMORY, "%s: Error allocating %d bytes for %s",
  102.          (int)facP, size, (int)nameP );
  103.  
  104.     return( (void *)newP );
  105. }
  106. /*
  107.  
  108. *//* erealloc( facP, nameP, oldP, size )
  109.  
  110.     Reallocate memory, give error if failure.
  111.  
  112. Accepts :
  113.  
  114.     facP        Name of facility (e.g., subroutine name)
  115.     nameP        Name of item being allocated
  116.     oldP        Ptr to old thing (may be NULL)
  117.     size        Desired size
  118.  
  119. Returns :
  120.  
  121.     < value >    Ptr to thing.
  122.  
  123. Notes :
  124.  
  125.     error return is taken if allocation fails.
  126. */
  127.  
  128. void *
  129. erealloc( facP, nameP, oldP, size )
  130.     char        *facP;        /* Ptr to facility name */
  131.     char        *nameP;        /* Ptr to name of thing */
  132.     char        *oldP;        /* Ptr to existing thing */
  133.     int        size;        /* Number of bytes */
  134. {
  135.     char        *newP;        /* Ptr to memory */
  136.  
  137.     if ( oldP == NULL )
  138.     newP = malloc( size );
  139.     else
  140.     newP = realloc( oldP, size );
  141.  
  142.     if ( newP == NULL )
  143.     error( EC_MEMORY, "%s: Error re-allocating %d bytes for %s",
  144.          (int)facP, size, (int)nameP );
  145.  
  146.     return( (void *)newP );
  147. }
  148. /*
  149.  
  150. *//* gettkline( fP, bufP, bufsize, tokcP, tokv, tokmax )
  151.  
  152.     Input a line of text and tokenize it.
  153.  
  154. Accepts :
  155.  
  156.     fP        Stream for input file
  157.     bufP        Buffer for line
  158.     bufsize        Size of the buffer
  159.     tokcP        Ptr to tokc variable -- # of tokens
  160.     tokv        Ptr to tokv array -- ptrs to tokens
  161.     tokmax        Maximum number of tokens to return
  162.     
  163. Returns :
  164.  
  165.     <value>        Number of characters read (-1 if EOF)
  166.     *tokcP        Number of tokens received
  167.     *tokv        Ptrs to tokens
  168.  
  169. Notes :
  170.  
  171.     Excess input (up to a newline) is ignored.
  172.  
  173.     The line buffer is used for the token buffer as well.
  174.  
  175. */
  176.  
  177. int
  178. gettkline( fP, bufP, bufsize, tokcP, tokv, tokmax )
  179.     FILE        *fP;        /* Input file ptr */
  180.     char        *bufP;        /* Buffer ptr */
  181.     int        bufsize;    /* Room in buffer */
  182.     int        *tokcP;        /* Variable to hold token count */
  183.     char        **tokv;        /* Arg vectors */
  184.     int        tokmax;        /* Max # tokens */
  185. {
  186.     int        cC;        /* Characters */
  187.  
  188.     /* Input the line */
  189.     cC = getline( fP, ++bufP, bufsize-1 );
  190.  
  191.     /* Use tkline() to tokenize it */
  192.     *tokcP = tkline( bufP, bufP-1, tokv, tokmax, "", " \t,:" );
  193.  
  194.     return( cC );
  195. }
  196. /*
  197.  
  198. *//* getline( fP, bufP, bufsize )
  199.  
  200.     Inputs a line of text from a file.
  201.  
  202. Accepts :
  203.  
  204.     fP        Stream for input file
  205.     bufP        Buffer for line
  206.     bufsize        Size of the buffer
  207.     
  208. Returns :
  209.  
  210.     <value>        Number of characters read (-1 if EOF)
  211.  
  212. Notes :
  213.  
  214.     Excess input (up to a newline) is ignored.
  215.  
  216. */
  217. getline( fP, bufP, bufsize )
  218. AREG1    FILE        *fP;        /* Input file ptr */
  219. AREG2    char        *bufP;        /* Buffer ptr */
  220.     int        bufsize;    /* Room in buffer */
  221. {
  222. DREG1    int        cC;        /* Characters */
  223. DREG2    int        ch;        /* Character */
  224.  
  225.     for( --bufsize, cC = 0; ( ( ch = getc( fP ) ) != '\n' ) && ( ch != EOF ); )
  226.         if ( cC++ < bufsize )
  227.         *bufP++ = ch;
  228.  
  229.     *bufP = NUL;
  230.     if ( ( cC == 0 ) && ( ch == EOF ) )
  231.     cC = -1;
  232.  
  233.     return( cC );
  234. }
  235.  
  236.  
  237. /*
  238.  
  239. *//* error( code, fmtP, args... )
  240.  
  241.     One-up interface to return_error
  242.  
  243. Accepts :
  244.  
  245.     code        Status code
  246.     fmtP        printf-style format string
  247.     args        printf arguments.  Quantity is limited.
  248.  
  249. Returns :
  250.  
  251.     Nothing.  Formats the string and calls return_error.
  252.  
  253. Notes :
  254.  
  255.     Handling of the arguments is kludgy.
  256.  
  257. */
  258.  
  259. #ifdef __TURBOC__
  260.     void error ( int code, char *fmtP, int    a1, int a2, int a3 )
  261. #else
  262. void
  263. error( code, fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 )
  264.     int        code;        /* Status code */
  265.     char        *fmtP;        /* message format string */
  266.     int        a1, a2, a3,    /* Like I said... */
  267.             a4, a5, a6,    /*  ... it's a kludge. */
  268.             a7, a8, a9, a10;
  269. #endif
  270. {
  271.     sprintf( &Errbuf[0], fmtP, a1, a2, a3
  272. #ifndef __TURBOC__
  273.     , a4, a5, a6, a7, a8, a9, a10
  274. #endif
  275.      );
  276.  
  277.     fprintf( stderr, "%d: %s.\n", code, Errbuf );
  278.     exit( 1 );
  279. }
  280. /*
  281.  
  282. *//* warning( code, fmtP, args... )
  283.  
  284.     Issue a warning (a la error(), but using note_error)
  285.  
  286. Accepts :
  287.  
  288.     code        Status code
  289.     fmtP        printf-style format string
  290.     args        printf arguments.  Quantity is limited.
  291.  
  292. Returns :
  293.  
  294.     Nothing; calls note_error and returns.
  295.  
  296. Notes :
  297.  
  298.     Handling of the arguments is kludgy.
  299.  
  300. */
  301.  
  302. void
  303. warning( code, fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 )
  304.     int        code;        /* Status code */
  305.     char        *fmtP;        /* message format string */
  306.     int        a1, a2, a3,    /* Like I said... */
  307.             a4, a5, a6,    /*  ... it's a kludge. */
  308.             a7, a8, a9, a10;
  309. {
  310.     sprintf( &Errbuf[0], fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 );
  311.     fprintf( stderr, "%d: %s.\n", code, Errbuf );
  312. }
  313.